package com.guotd.jspwork2.controller;

import com.guotd.jspwork2.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/editKind")
public class editKindServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String desc = request.getParameter("desc");
        String kindName = request.getParameter("kind");
        int kindId = Integer.parseInt(request.getParameter("kindId"));
        String sql = "update kind set desc=?,kind=? WHERE id=?";
        String[] args = {desc,kindName,String.valueOf(kindId)};
        int row = JDBCUtil.update(sql, args);
        if (row > 0) {
            request.getSession().setAttribute("kind_msg", "修改成功");
            response.sendRedirect("getAllKinds");
        } else {
            request.setAttribute("kind_msg", "修改失败");
        }
    }
}
